package com.example.hm_dtfkz.app;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.util.Log;
import android.webkit.WebView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.android.base.util.ToastUtil;
import com.android.base.util.http.RxhttpUtil;
import com.dq.ad.AdManager;
import com.example.hm_dtfkz.config.AppConfig;
import com.example.hm_dtfkz.ui.activity.LoginActivity;
import com.example.hm_dtfkz.ui.activity.SplashActivity;
import com.example.hm_dtfkz.util.AppFrontBackHelper;
import com.example.hm_dtfkz.util.DQLog;
import com.example.hm_dtfkz.util.HomeReceiver;
import com.example.hm_dtfkz.util.Mmkv;
import com.example.hm_dtfkz.util.SystemUtil;
import com.example.hm_dtfkz.util.sound.MediaBackManager;
import com.kc.openset.config.OSETSDK;
import com.kc.openset.listener.OSETInitListener;

import org.litepal.LitePal;

import java.util.Random;

/**
 * FileName: QuestionsApp
 * Author: admin
 * Date: 2021/11/1 16:26
 * Description:
 */
public class QuestionsApp extends Application {
    public static Context context;
    public static QuestionsApp questionsApp;

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            Log.e("aaaaaa", "进程名：" + getProcessName());
            // 安卓9.0后不允许多进程使用同一个数据目录
            try {
                WebView.setDataDirectorySuffix(getProcessName());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
        questionsApp = this;
        BaseLibrary.init(this);
        com.android.base.util.Mmkv.getInstance().init(this);
        LitePal.initialize(this);
        // userId传你们的用户Id，有就传没有就不传，用于根据用户反馈问题，查问题用的


        //如果明确某个进程不会使用到广告SDK，可以只针对特定进程初始化广告SDK的content
        //if (PROCESS_NAME_XXXX.equals(processName)) {
        //   TTAdSdk.init(context, config);
        //}
        ARouter.init(this);
        if (SystemUtil.isToday()) {//今天第二次第N次打开
            DQLog.e("今天第N次打开");
            if (SystemUtil.is8()) {//每天第N次打开，并且在8点后
                if (!AppConfig.getResetOnlineState()) {//如已经重置过，不再进行重置
                    reset();
                }
            } else {
                AppConfig.setResetOnlineState(false);
            }
        } else {//今天第一次打开
            SystemUtil.setToday();
            DQLog.e("今天第一次打开");
            AppConfig.setDayOnLineTime(0);      //当天在线时长
            int loginDay = AppConfig.getLoginDayNumber() + 1;
            AppConfig.setLoginDayNumber(loginDay);  //累计登陆天数

            if (SystemUtil.is8()) {//每天第一次打开，并且在8点后
                reset();
            } else {
                AppConfig.setResetOnlineState(false);
            }
        }

        if (!Mmkv.contains("total_piggy")) {
            AppConfig.setTotalPiggy(1000000);
        }
        if (!Mmkv.contains("cash_cow")) {
            AppConfig.setCashCow(0.0);
        }
        if (!Mmkv.contains("remain_withdrawal_times")) {
            AppConfig.setRemainWithdrawalTimes(10);
        }

        if (!Mmkv.contains("ad_recommend")) {
//            DQLog.e("set ad recommend state");
            Mmkv.setBoolean("ad_recommend", true);
        }

        AppFrontBackHelper.getInstance().register(this, new AppFrontBackHelper.OnAppStatusListener() {
            @Override
            public void onFront(Activity activity) {
                //应用切到前台处理
                if (!(activity instanceof SplashActivity) && !(activity instanceof LoginActivity)) {
                    if (HomeReceiver.isBackground) {
                        HomeReceiver.isBackground = false;
                        Intent intent = new Intent(activity, SplashActivity.class);
                        intent.putExtra("key_from", "app");
                        activity.startActivity(intent);
                    }
                }
            }

            @Override
            public void onBack(Activity activity) {
                if (AppConfig.getBackMusic()) {
                    MediaBackManager.getInstance(getApplicationContext()).pause();
                }
                if (HomeReceiver.isHome(getApplicationContext())) {
                    HomeReceiver.isBackground = true;
                }
            }
        });
    }

    private void reset() {
        AppConfig.setDayAnswerNumber(0);
        AppConfig.setResetOnlineState(true);
        AppConfig.setExtractTimes(0);
//        AppConfig.setShowDaily1(false);
//        AppConfig.setShowDaily2(false);
        int nextInt = new Random().nextInt(400) + 800;
        AppConfig.setTodayPeople(nextInt);
        AppConfig.setRemainWithdrawalTimes(10);
    }
}